# Maintainer: Felix Yan <felixonmars@archlinux.org>
# Maintainer: Caleb Maclennan <caleb@alerque.com>

_hkgname=pandoc-lua-engine
pkgname=haskell-pandoc-lua-engine
pkgver=0.2.0.1
pkgrel=15
pkgdesc="Lua engine to power custom pandoc conversions"
url="https://pandoc.org"
license=("GPL")
arch=('loong64' 'x86_64')
depends=('ghc-libs' 'haskell-sha' 'haskell-aeson' 'haskell-citeproc' 'haskell-data-default'
         'haskell-doclayout' 'haskell-doctemplates' 'haskell-hslua' 'haskell-hslua-module-doclayout'
         'haskell-hslua-module-path' 'haskell-hslua-module-system' 'haskell-hslua-module-text'
         'haskell-hslua-module-version' 'haskell-hslua-module-zip' 'haskell-hslua-repl'
         'haskell-lpeg' 'haskell-pandoc' 'haskell-pandoc-lua-marshal' 'haskell-pandoc-types')
makedepends=('ghc' 'uusi' 'haskell-tasty' 'haskell-tasty-golden' 'haskell-tasty-hunit'
             'haskell-tasty-lua')
source=("https://hackage.haskell.org/packages/archive/$_hkgname/$pkgver/$_hkgname-$pkgver.tar.gz")
sha256sums=('8a3899128caf1eb12643a25a4fbe9d02cbfa08aa8aa7698d5f172ed2a9a031f8')

prepare() {
  cd $_hkgname-$pkgver
  gen-setup
}

build() {
  cd $_hkgname-$pkgver

  runhaskell Setup configure -O --enable-shared --enable-executable-dynamic --disable-library-vanilla \
    --prefix=/usr --docdir=/usr/share/doc/$pkgname --datasubdir=$pkgname --enable-tests \
    --dynlibdir=/usr/lib --libsubdir=\$compiler/site-local/\$pkgid --ghc-option=-fllvm \
    --ghc-option=-optl-Wl\,-z\,relro\,-z\,now \
    --ghc-option='-pie' 

  runhaskell Setup build $MAKEFLAGS
  runhaskell Setup register --gen-script
  runhaskell Setup unregister --gen-script
  sed -i -r -e "s|ghc-pkg.*update[^ ]* |&'--force' |" register.sh
  sed -i -r -e "s|ghc-pkg.*unregister[^ ]* |&'--force' |" unregister.sh
}

check() {
  cd $_hkgname-$pkgver
  # https://github.com/jgm/pandoc/pull/8898
  runhaskell Setup test --show-details=direct || echo "Tests failed"
}

package() {
  cd $_hkgname-$pkgver

  install -D -m744 register.sh "$pkgdir"/usr/share/haskell/register/$pkgname.sh
  install -D -m744 unregister.sh "$pkgdir"/usr/share/haskell/unregister/$pkgname.sh
  runhaskell Setup copy --destdir="$pkgdir"
  install -D -m644 COPYING.md -t "$pkgdir"/usr/share/licenses/$pkgname/
  rm -f "$pkgdir"/usr/share/doc/$pkgname/COPYING.md
}
